home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 September / Macworld (1998-09).dmg / Shareware World / Info / For Developers / MacZoop 1.8.3 / More Classes / Window Classes / TextStyleUtils.c < prev    next >
Text File  |  1998-07-09  |  3KB  |  122 lines

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *            TextStyleUtils.c    - utilties for dealing with styled TextEdit
  8. *
  9. *
  10. *            © 1998, Graham Cox
  11. *
  12. *
  13. *
  14. *************************************************************************************************/
  15.  
  16. #include    "TextStyleUtils.h"
  17.  
  18.  
  19. void    TEGetStyleRunInfo( TEStyleRunInfo* runInfo, TEHandle te )
  20. {
  21.     if ( te == NULL || runInfo == NULL )
  22.         return;
  23.         
  24.     // initialise fields of record:
  25.     
  26.     runInfo->fr = 0;
  27.     runInfo->runStyles = 0;
  28.     runInfo->contStyles = 0xFF;
  29.     runInfo->numSizes = 0;
  30.     runInfo->numFonts = 0;
  31.     
  32.     // get the run start and finish:
  33.     
  34.     short        st, en, lh, as;
  35.     TextStyle    ts;
  36.     
  37.     st = (*te)->selStart;
  38.     en = (*te)->selEnd;
  39.     
  40.     // we're going to go through from start to end getting the text style of every character
  41.     
  42.     do
  43.     {
  44.         TEGetStyle( st, &ts, &lh, &as, te );
  45.         
  46.         // use our own flag if plain style
  47.         
  48.         if ( ts.tsFace == 0 )
  49.             ts.tsFace = kPlainStyle;
  50.         
  51.         // for the face, we just need to OR what we got with what we already have.
  52.         // to set the continuity flag, just clear it if the face bit is 0.
  53.         
  54.         runInfo->runStyles |= ts.tsFace;
  55.         runInfo->contStyles &= ts.tsFace;
  56.         
  57.         // for the font and size, we need to do more:
  58.         
  59.         if ( ! FontInRun( ts.tsFont, runInfo, &as ))
  60.             runInfo->fonts[ runInfo->numFonts++ ] = ts.tsFont;
  61.         
  62.         if ( ! SizeInRun( ts.tsSize, runInfo, &as ))
  63.             runInfo->sizes[ runInfo->numSizes++ ] = ts.tsSize;
  64.     }
  65.     while( ++st < en );
  66.     
  67.     // finally set the flags for continous font and size. These are true if the
  68.     // corresponding count is 0 or 1.
  69.     
  70.     if ( runInfo->numFonts < 2 )
  71.         runInfo->fr |= kFontIsContinuous;
  72.         
  73.     if ( runInfo->numSizes < 2 )
  74.         runInfo->fr |= kSizeIsContinuous;
  75. }
  76.  
  77.  
  78. Boolean    FontInRun( short aFont, TEStyleRunInfo* runInfo, short* index )
  79. {
  80.     // check whether the given font is in the run or not
  81.     
  82.     short    i;
  83.     
  84.     if ( runInfo->numFonts >= MAX_FONTS )
  85.         return TRUE;
  86.     
  87.     for ( i = 0; i < runInfo->numFonts; i++ )
  88.     {
  89.         if ( runInfo->fonts[i] == aFont )
  90.         {
  91.             *index = i;
  92.             return TRUE;
  93.         }
  94.     }
  95.     
  96.     *index = runInfo->numFonts;
  97.     return FALSE;
  98. }
  99.  
  100.  
  101. Boolean    SizeInRun( short aSize, TEStyleRunInfo* runInfo, short* index )
  102. {
  103.     // check whether the given size is in the run or not
  104.     
  105.     short    i;
  106.     
  107.     if ( runInfo->numSizes >= MAX_SIZES )
  108.         return TRUE;
  109.     
  110.     for ( i = 0; i < runInfo->numSizes; i++ )
  111.     {
  112.         if ( runInfo->sizes[i] == aSize )
  113.         {
  114.             *index = i;
  115.             return TRUE;
  116.         }
  117.     }
  118.     
  119.     *index = runInfo->numSizes;
  120.     return FALSE;
  121. }
  122.